import pandas as pd
import numpy as np
import torch
import scipy.sparse as sp

#稀疏矩阵的归一化
#mx形式：只有一列，列元素间用'\t'的多个元素，每个元素中有大量的0
def normalize_features(mx):
    """Row-normalize sparse matrix"""
    # rowsum = np.array(mx.sum(1))#行号-按列求和
    # r_inv = np.power(rowsum, -1).flatten()#flatten()返回一个一维数组

    #2021年12月17日，np.power(rowsum, -1)中rowsum不可以是int，要是float
    rowsum = np.array(mx.sum(1),dtype=float)#行号-按列求和
    r_inv = np.power(rowsum, -1).flatten()#flatten()返回一个一维数组

    r_inv[np.isinf(r_inv)] = 0.#无穷置0
    r_mat_inv = sp.diags(r_inv)#按行构造对角矩阵
    mx = r_mat_inv.dot(mx)
    return mx

path='../FAGCN/high_freq/new_squirrel/features.txt'#仅一列，5201个索引

features = np.loadtxt(path, dtype=float)
# print(features[0].sum())#45
# print(features.sum())#137689.0
features = normalize_features(features)#特征值归一化
features = torch.FloatTensor(features)
# print(features[0])
# print(features[0].sum())#第一列求和；归一化后为tensor(1.)
print(features.dtype,features.size())#torch.float32 torch.Size([5201, 3148])


